set rmsg on
set more off

************************************************************************
*
*	NOTE:		This do-file uses as input the final regression sample
*				and estimates equity and debt issuance regressions.
*
*************************************************************************

capture program drop _all
clear
clear matrix


* Define paths

	global 	data	"..."
			
	
*************************************************************************
**** Begin: Equity issuance ****
*************************************************************************	

	use "$data/GMMSV_reg_sample.dta", clear

**	1. Define control variables
	global c sd_indcf5_lagatl1 l_al1 cf5_lagatl1 mbresid5

**	2. Define cash issuance dummies	
	g dummy1 = 1 if ch_cash_at > 0 & ch_cash_at != .

**	3. Regressions
**	3.1 Net Debt + Equity 
	g depvar = net_ltdebt_issue_at + net_equity_issue2_at
	
	* OLS
	areg depvar rho sigma_P sigma_A sd_indcf5_lagatl1 l_al1 cf5_lagatl1 mbresid5 i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
	est store ols
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
	est store econ_ols
	
	* IFE
	areg depvar rho sigma_P sigma_A sd_indcf5_lagatl1 l_al1 cf5_lagatl1 mbresid5 i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
	est store ife
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
	est store econ_ife

**	3.2 Net External finance > 0 
	drop depvar
	g depvar = max(ltdebt_issue_dummy,eq_issue_dummy) 
	replace depvar = . if ltdebt_issue_dummy ==. | eq_issue_dummy == .
	
	* OLS
	*reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(cluster sic3)
	areg depvar rho sigma_P sigma_A sd_indcf5_lagatl1 l_al1 cf5_lagatl1 mbresid5 i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
	est store olsprob
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
	est store econ_olsprob

	* IFE
	areg depvar rho sigma_P sigma_A sd_indcf5_lagatl1 l_al1 cf5_lagatl1 mbresid5 i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
	est store ifeprob
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
	est store econ_ifeprob
		
**	3.3 Net Debt and Equity issuance
	g depvar1  =  net_equity_issue2_at
	g depvar2  =  net_ltdebt_issue_at

	sureg (Eq: depvar1 = rho sigma_P sigma_A $c  i.fyear i.sic2) (Dt: depvar2 = rho sigma_P sigma_A $c  i.fyear) if dummy1 == 1, corr
	est store sureg 
	
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar1 if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[Eq:sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[Eq:rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[Eq:sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[Eq:sigma_A]*sigA_sd/depvar_me) , post
	est store econ_sureg_eq
	
	* Econ significance: evaluation points
	est restore sureg 
	quietly summ depvar2 if e(sample), det
		scalar depvar_me = r(mean)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[Dt:sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[Dt:rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[Dt:sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[Dt:sigma_A]*sigA_sd/depvar_me) , post
	est store econ_sureg_dt
	
**	3.4 Net Debt and Equity issuance > 0
	drop depvar1 depvar2
	g depvar1  =  eq_issue_dummy
	g depvar2  =  ltdebt_issue_dummy

	sureg (Eq: depvar1 = rho sigma_P sigma_A $c  i.fyear i.sic2) (Dt: depvar2 = rho sigma_P sigma_A $c  i.fyear) if dummy1 == 1, corr
	est store dsureg 	
	
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar1 if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[Eq:sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[Eq:rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[Eq:sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[Eq:sigma_A]*sigA_sd/depvar_me) , post
	est store econ_dsureg_eq
	
	* Econ significance: evaluation points
	est restore dsureg 
	quietly summ depvar2 if e(sample), det
		scalar depvar_me = r(mean)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[Dt:sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[Dt:rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[Dt:sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[Dt:sigma_A]*sigA_sd/depvar_me) , post
	est store econ_dsureg_dt	
	

**	3.5 Tabulate Results

	label var rho "Rho"
	label var sigma_P "Sigma_P: Permanent volatility"
	label var sigma_A "Sigma_A: Transitory volatility"
	*label var sigA_alpha "Sigma_A / Alpha"
	label var mu "Mu: Growth rate of asset productivity"
	label var sd_indcf5_atl1 "Lagged cash flow-to-assets volatility"
	label var l_al1 "Lagged log(total assets)"
	label var cf5_atl1 "Lagged cash flow-to-assets ratio"
	label var cf5_at "Cash flow-to-lagged assets ratio"
	label var mbl1 "Lagged MB ratio"
	label var mbresid "Residuals of lagged MB ratio"
	
cd "$data"

	estout ols ife olsprob ifeprob sureg dsureg using "Table10.txt", replace 	///
		   mlabels("Net.OLS" "Net.IFE" "1{N>0}.OLS" "1{N>0}.IFE" "sureg" "dsureg") num unstack ///
		   cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a, fmt(%9.3g %9.0g) labels(Observations R2 "Adjusted R2")) ///
		   legend  label varlabels(_cons Constant) abbrev starl(* 0.1 ** 0.05 *** 0.01) ///
		   order(rho sigma_P sigma_A) ///
		   title("Table: Dependent variables are Total Net Debt plus Equity issuance to assets (cols 1,2), whether these totals are positive or zero (1{N>0}, in cols 3,4) or Net Debt and Net Equity issuance (totals cols 5,6 and dummies 7,8). All samples are conditional on cash balances being topped up. Clustering of SEs at the SIC3 level.") ///	
		   varwidth(45) modelwidth(10) delimiter("") style(fixed) drop(*fyear* *sic2*) wrap 
		   
	estout  econ_ols econ_ife econ_olsprob econ_ifeprob econ_sureg_eq econ_sureg_dt econ_dsureg_eq econ_dsureg_dt using "Table10.txt", append 		///
		   cells( b(star fmt(%9.3f)) se(par)) ///
		   legend label abbrev starl(* 0.1 ** 0.05 *** 0.01) ///
		   order(RMEffrho RMEffsigP RMEffsigA RMEffCashVol) ///
		   title("Table: Panel B. Economic effects: MEffVar is Delta E(y) = dE(y)/d(Var) * Std(Var), RMEffVar is MEffVar / E(y)") ///	
		   varwidth(45) modelwidth(10) delimiter("") style(fixed) wrap	

		   
